package org.domus.sipnmove.useragent;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.SwingUtilities;

import org.domus.sipnmove.useragent.impl.UserAgentImpl;
import org.domus.sipnmove.useragent.view.UserAgentView;

/**
 * Main class of the user agent
 */
public class Main {

	private static UserAgentImpl userAgent;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			Integer port = 5060;
			String ipAddress = InetAddress.getLocalHost().getHostAddress();

			// User name is arg 0 and required
			if (args.length < 1) {
				System.err.println("Invalid user name");
				System.exit(0);
			}

			UserInformation userInformation = new UserInformation();
			userInformation.setName(args[0]);

			// Port number is arg 1 and default value is 5060
			if (args.length > 1) {
				port = new Integer(args[1]);

				if (port == null) {
					System.err.println("Invalid port argument (maybe not a number)");
					System.exit(0);
				}
			}

			userInformation.setPort(port);

			// IP listen address is arg 2 and default value is localhost
			if (args.length > 2) {
				ipAddress = args[2];
			}

			userInformation.setIpAddress(ipAddress);

			// Creation of the user agent and the view associated
			userAgent = new UserAgentImpl(userInformation);

			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					UserAgentView userAgentView = new UserAgentView(userAgent);
					userAgent.addUserAgentObserver(userAgentView);
					userAgentView.setVisible(true);
				}
			});

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
